from plyer import notification

from MonitorTool.douyin import DouYingMonitor
from MonitorTool.gongzhonghao import GongZhongHaoMonitor
from MonitorTool.vx_shipinhao import VXShiPinHaoMonitor
from MonitorTool.xiaohongshu import XiaoHongShuMonitor
from MonitorTool.zhihu import ZhiHuMonitor
from PublishTool.douyin import DouYingPublish
from PublishTool.gongzhonghao import GongZhongHaoPublish
from PublishTool.vx_shipinhao import VXShiPinHaoPublish
from PublishTool.xiaohongshu import XiaoHongShuPublish
from PublishTool.zhihu import ZhiHuPublish
from config.config import MainConfig, SeleniumConfig
from lib.chrome import BrowserDev
from lib.wifi import Wifi
from utils.logUtils import log_util


class ToolFactory:
    """
    工具工厂
    """

    def __init__(self):
        self.tool = None
        self.wifi = Wifi()
        self.browserDev = BrowserDev()  # 浏览器对象，工厂
        self.driver = None
        self.tool = {
            "publish": {
                "抖音": DouYingPublish,
                "微信公众号": GongZhongHaoPublish,
                "知乎": ZhiHuPublish,
                "微信视频号": VXShiPinHaoPublish,
                "小红书": XiaoHongShuPublish
            },
            "monitor": {
                "小红书": XiaoHongShuMonitor,
                "微信视频号": VXShiPinHaoMonitor,
                "知乎": ZhiHuMonitor,
                "微信公众号": GongZhongHaoMonitor,
                "抖音": DouYingMonitor
            }
        }

    def init_driver_wifi(self, phone_number, ssid, isMonitor=False):
        # 切换wifi
        flag = self.wifi.switch_wifi(ssid)
        if flag:
            if not self.wifi.is_internet():
                log_util.tk_log(f"切换wifi:{ssid} 成功，但无法连接网络 账号:{phone_number}")
                raise Exception(f"切换wifi:{ssid} 成功，但无法连接网络 账号:{phone_number}")
        else:
            log_util.tk_log(f"切换wifi{ssid}失败！ 账号:{phone_number}")
            raise Exception(f"切换wifi:{ssid} 失败 账号:{phone_number}")

        # 初始化浏览器对象
        self.driver = self.browserDev.init(phone_number, isMonitor)

    def get_tool(self, platform, is_publish=True, default=None):
        """
        获取发布工具
        :param platform: 平台
        :param is_publish: 是否是发布工具
        :param default: 默认值
        :return: 获取对应的工具
        """
        if is_publish: return self.tool['publish'].get(platform, default)(self.driver)
        return self.tool['monitor'].get(platform, default)(self.driver)


def notice(message):
    title = '鹅有招·自动发布工具'
    notification.notify(title=title, message=message, app_name='鹅有招 自动发布工具', timeout=MainConfig.notification * 1000)
